﻿using System;
using UnityEngine;

public enum EPLATFORM
{
    PLAT_NULL = 0,
    PLAT_TENCENT = 1,
    PLAT_AISI = 2,
    PLAT_KUAIYONG = 3,
    PLAT_IAPPLE = 4,
    PLAT_XYHANDER = 5,
    PLAT_PPHANDER = 6,
    PLAT_HAIMA = 7,
    PLAT_TONGBUTUI = 8,
    PLAT_GUOPAN = 9,
    PLAT_SINA = 10,
    PLAT_360 = 11,
    PLAT_UC = 12
}

public enum ERUNPLATFORM
{
    ERUN_IOS = 0,
    ERUN_ANDROID = 1,
    ERUN_OTHER = 2,
}

public class PayParams
{
    public string ProductId { get; set; }           //商品id
    public string ProductName { get; set; }         //商品名
    public int Price { get; set; }                  //价格
    public int BuyNum { get; set; }                 //购买数量
    public int CoinNum { get; set; }                //金币数量
    public string ServerId { get; set; }            //服务器id（应该是用于分区）
    public string RoleId { get; set; }              //角色ID
    public string RoleName { get; set; }            //角色名
    public int RoleLevel { get; set; }              //角色等级
    public string AppName { get; set; }             //应用名
    public int ExchangeRate { get; set; }           //人民币与游戏充值币的比例，例如2，代表1元人民币可以兑换2个游戏币，整数。
    public string PayCallBackURL { get; set; }      //支付结果通知接口
    public string OrderNum { get; set; }            //订单号
}

public class PlatFormMgr : MonoBehaviour
{
    public bool bLogIn;
    public string strInfo;

    public string deviceToken;		//app Token
    public string sysVersion;		//系统版本
    public string appVersion;		//app版本
    public string bundleidentifier; //bundle id
    public string deviceModle;		//app名字

    void Awake()
    {
        ZEventCenter.AddEventListener(ConstData.PROGRESS_UPDATE, OnLoading);
        ZEventCenter.AddEventListener(ConstData.PROGRESS_OK, LoadComplete);
    }
    
    void OnGUI()
    {
        GUI.color = Color.red;  // 文本颜色显示
        GUI.Label(new Rect(50, 200, 200, 100), strInfo);
    }

    /// <summary>
    /// 初始化平台信息
    /// </summary>
    public void InitPaltForm()
    {

    }

    /// <summary>
    /// 显示平台登录界面
    /// </summary>
    public void ShowLogIn()
    {

    }

    /// <summary>
    ///（初次登陆显示注册界面）
    /// </summary>
    /// <param name="msg"></param>
    public void OnInitSuccess(string msg)
    {
        Debug.Log("OnInitSuccess    " + msg);
        strInfo += ("OnInitSuccess    " + msg);
    }

    /// <summary>
    /// 平台初始化完成之后显示登录界面
    /// </summary>
    /// <param name="msg"></param>
    public void OnIsLogined(string msg)
    {
        Debug.Log("OnIsLogined    " + msg);
        strInfo += ("OnIsLogined    " + msg);
    }

    /// <summary>
    /// 用户id
    /// </summary>
    /// <param name="msg"></param>
    public void OnUserID(string msg)
    {
        Debug.Log("OnUserID    " + msg);
    }

    /// <summary>
    /// 用户名字
    /// </summary>
    /// <param name="msg"></param>
    public void OnUserName(string msg)
    {
        Debug.Log("OnUserName    " + msg);
        if (bLogIn)
        {
            bLogIn = false;
        }
    }
    
    /// <summary>
    /// 充值结果回调
    /// </summary>
    /// <param name="msg"></param>
    public void PayAndRechangeResult(string msg)
    {
        Debug.Log("payAndRechangeResult    " + msg);
    }

    /// <summary>
    /// 平台登录成功
    /// </summary>
    /// <param name="msg"></param>
    public void OnLoginSuccess(string msg)
    {
        Debug.Log("OnLoginSuccess    " + msg);
        strInfo += ("OnLoginSuccess    " + msg);
        
        char[] split = new char[] { ':' };
        string[] result = msg.Split(split, StringSplitOptions.RemoveEmptyEntries);
        if (result.Length == 3)
        {
            PlayerData.GetSingleton().userName = result[0];
            PlayerData.GetSingleton().loginToken = result[1];
            strInfo += ("    " + PlayerData.GetSingleton().userName);
        }

        //登录服务器
        PlayerData.GetSingleton().platForm = GetCurPlatform();
        //内网测试版本，可选服务器
        bLogIn = true; ServerMgr.GetSingleton().SaveLastServer();
        PlayerData.GetSingleton().gateHost = ServerMgr.GetSingleton().GetCurServerIP();

        strInfo += PlayerData.GetSingleton().gateHost;
        Debug.Log(PlayerData.GetSingleton().gateHost + " 1 " + PlayerData.GetSingleton().loginToken + " 1 " + PlayerData.GetSingleton().userName);
        UpdateMgr.GetSingleton().AddUpdate(GameClient.GetSingleton());
        GameClient.GetSingleton().ConnectGate();
    }
    
    /// <summary>
    /// 
    /// </summary>
    public void DoLogin()
    {
        
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void OnLoginFailed(string msg)
    {
        Debug.Log("OnLoginFailed    " + msg);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void OnCloseLoginView(string msg)
    {
        Debug.Log("OnCloseLoginView    " + msg);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void OnCloseRegisterView(string msg)
    {
        Debug.Log("OnCloseRegisterView    " + msg);
    }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void OnLoginOut(string msg)
    {
        bLogIn = false;
        ZMgr.FaceCenter.LogOutToLoginView();
    }

    /// <summary>
    /// 退出登录成功，返回登录界面
    /// </summary>
    /// <param name="msg"></param>
    public void OnLogoutSuccess(string msg)
    {
        bLogIn = false;
        ZMgr.FaceCenter.LogOutToLoginView();
    }

    /// <summary>
    /// 
    /// </summary>
    public void LogOut()
    {
        bLogIn = false;
        ZMgr.FaceCenter.LogOutToLoginView();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="amount">充值数目</param>
    /// <param name="paramBillNo">交易号</param>
    /// <param name="paramBillTitle">交易标题</param>
    /// <param name="paramRoleId">角色id</param>
    /// <param name="zoneID">区号</param>
    public void PayRmb(int amount, string paramBillNo, string paramBillTitle, string paramRoleId, int zoneID)
    {
        
    }

    /// <summary>
    /// 用户支付
    /// </summary>
    /// <param name="payParams"></param>
    public void PayMoney(PayParams payParams)
    {
        
    }

    /// <summary>
    /// 支付成功
    /// </summary>
    /// <param name="msg"></param>
    public void OnPaySuccess(string msg)
    {
        EDebug.Log(msg);
    }

    /// <summary>
    /// 支付失败
    /// </summary>
    /// <param name="msg"></param>
    public void OnPayFailed(string msg)
    {
        EDebug.Log(msg);
    }
    
    /// <summary>
    /// 注销SDK(退出游戏前调用)
    /// </summary>
    public void OnExitSDK()
    {

    }

    /// <summary>
    /// 注销SDK成功
    /// </summary>
    public void ExitSDKSuccess(string msg)
    {
        EDebug.Log(msg);
        Application.Quit();
    }

    /// <summary>
    /// 注销SDK失败
    /// </summary>
    /// <param name="msg"></param>
    public void ExitSDKFailed(string msg)
    {
        EDebug.Log(msg);
    }

    /// <summary>
    /// 如果正在加载，隐藏悬浮条
    /// </summary>
    public void OnLoading(ZEvent ev)
    {
        
    }

    /// <summary>
    /// 如果加载完成，显示悬浮条
    /// </summary>
    public void LoadComplete(ZEvent ev)
    {

    }

    /// <summary>
    /// 这个是ios系统的一些系统参数，用于做推送处理的
    /// </summary>
    /// <param name="strInfo"></param>
    public void OnDeviceToken(string strInfo)
    {
        char[] split = new char[] { ':' };
        string[] result = strInfo.Split(split, StringSplitOptions.RemoveEmptyEntries);
        if (result.Length == 5)
        {
            deviceToken = result[0];
            sysVersion = result[1];
            appVersion = result[2];
            bundleidentifier = result[3];
            deviceModle = result[4];
        }
        EDebug.Log(strInfo + " devicetoken : " + deviceToken + "    sysversion : " + sysVersion 
            + "  appversion: " + appVersion + "  bundleid:" + bundleidentifier + "   devicemode:" + deviceModle);
    }

    /// <summary>
    /// 提交游戏数据(角色ID，角色昵称，角色等级，区服id，区服名称，角色创建时间*单位秒，长度10，是服务器存储的时间*)
    /// </summary>
    public void SubmitExtendData(string roleId, string roleName, string roleLevel, string zoneId, string zoneName, long roleCTime)
    {

    }

    private Rect GetRectByNo(int no)
    {
        return new Rect((Screen.width / 2 - (2 - no % 5) * 180 + 70), 70 * (no / 3) + 50, 160, 60);
    }

    public static void DebugPlatLog()
    {
#if TENCENT
        EDebug.Log("TENCENT");
#elif AISI
        EDebug.Log("AISI");
#elif KUAIYONG
        EDebug.Log("KUAIYONG");
#elif IAPPLE
        EDebug.Log("IIAPPLE");
#elif XYHANDER
        EDebug.Log("XYHANDER");
#elif PPHANDER
        EDebug.Log("PPHANDER");
#elif HAIMA
        EDebug.Log("HAIMA");
#elif TONGBUTUI
        EDebug.Log("TONGBUTUI");
#elif GUOPAN
		EDebug.Log("GUOPAN");
#elif SINA
		EDebug.Log("SINA");
#elif QIHU
        EDebug.Log("QIHU");
#elif UC
        EDebug.Log("UC");
#endif
    }

    public static ERUNPLATFORM GetRunSystemPlat()
    {
        ERUNPLATFORM plat = ERUNPLATFORM.ERUN_IOS;
        if (Application.platform == RuntimePlatform.OSXPlayer
            || Application.platform == RuntimePlatform.OSXEditor
            || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            plat = ERUNPLATFORM.ERUN_IOS;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            plat = ERUNPLATFORM.ERUN_ANDROID;
        }
        else
        {
            plat = ERUNPLATFORM.ERUN_OTHER;
        }
        return plat;
    }

    public static EPLATFORM GetCurPlatform()
    {
#if TENCENT
        return EPLATFORM.PLAT_TENCENT;
#elif AISI
        return EPLATFORM.PLAT_AISI;
#elif KUAIYONG
        return EPLATFORM.PLAT_KUAIYONG;
#elif IAPPLE
        return EPLATFORM.PLAT_IAPPLE;
#elif XYHANDER
        return EPLATFORM.PLAT_XYHANDER;
#elif PPHANDER
        return EPLATFORM.PLAT_PPHANDER;
#elif HAIMA
        return EPLATFORM.PLAT_HAIMA;
#elif TONGBUTUI
        return EPLATFORM.PLAT_TONGBUTUI;
#elif GUOPAN
		return EPLATFORM.PLAT_GUOPAN;
#elif SINA
		return EPLATFORM.PLAT_SINA;
#elif QIHU
        return EPLATFORM.PLAT_360;
#elif UC
        return EPLATFORM.PLAT_UC;
#else
        return EPLATFORM.PLAT_NULL;
#endif
    }
}